import * as AppConfigs from '@/configs/app';

import Fetch, { buildURL } from './fetch';

/**
 * 根据 id 获取子应用信息
 * @param {number|string} id 子应用 id
 * @returns {Promise<{code:any; msg:string; data:{agentId:number; realAgentId:number; cropId:string; name:string; homepageLink:string;}}>}
 */
export const GetSubAppConfig = (id) => {
  return Fetch(buildURL(`application-message/${id}`, true));
};

/**
 * 根据 code 获取应用信息
 * @returns {Promise<{code:any; msg:string; data:{agentId:number; realAgentId:number; cropId:string; name:string; homepageLink:string;}}>}
 */
export const GetAppConfig = (code = AppConfigs.APP_MARK_CODE) => {
  return Fetch(buildURL(`application-message/code/${code}`, true));
};

/**
 * 根据 agentId 获取应用信息
 * @returns {Promise<{code:any; msg:string; data:{agentId:number; realAgentId:number; cropId:string; name:string;}}>}
 */
export const GetAppConfigByAgentId = (agentId) => {
  return Fetch(buildURL(`application-message/agentId/${agentId}`, true));
};

/**
 * 上传图片
 * @param {FormData} file FormData 对象 file 字段对应文件
 * @returns {Promise<{code:any; msg:string; data:{name:string; shortUrl:string; url:string;}}>}
 */
export const PostImage = (formData) => {
  return Fetch(buildURL('imageinfo/upload'), formData, 'FORM_DATA');
};

/**
 * 上传图片识别二维码
 * @param {FormData} file FormData 对象 file 字段对应文件
 * @returns {Promise<{code:any; msg:string; data:{content:string;}}>}
 */
export const PostImage2QR = (formData) => {
  return Fetch(buildURL('imageinfo/qrcode-content'), formData, 'FORM_DATA');
};
